package cn.patient.pp.module.patients.controller.admin;

import cn.patient.pp.framework.excel.core.listener.TemplateCheckListener;
import cn.patient.pp.module.patients.controller.admin.vo.*;
import cn.patient.pp.module.patients.controller.admin.vo.importExecl.FollowUpExcelVO;
import cn.patient.pp.module.patients.controller.admin.vo.importExecl.ImportRespVO;
import cn.patient.pp.module.patients.dal.dataobject.patients.PatientsDODO;
import cn.patient.pp.module.patients.framework.web.config.Config;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.patient.pp.framework.common.pojo.PageParam;
import cn.patient.pp.framework.common.pojo.PageResult;
import cn.patient.pp.framework.common.pojo.CommonResult;
import cn.patient.pp.framework.common.util.object.BeanUtils;
import static cn.patient.pp.framework.common.pojo.CommonResult.success;

import cn.patient.pp.framework.excel.core.util.ExcelUtils;

import cn.patient.pp.framework.apilog.core.annotation.ApiAccessLog;
import static cn.patient.pp.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.patient.pp.module.patients.dal.dataobject.patients.FollowUpDO;
import cn.patient.pp.module.patients.service.FollowUpService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 随访列表")
@RestController
@RequestMapping("/business/follow")
@Validated
public class FollowUpController {

    @Resource
    private FollowUpService upService;

//    @PostMapping("/create")
//    @Operation(summary = "创建随访列表")
//    @PreAuthorize("@ss.hasPermission('follow:up:create')")
//    @ApiAccessLog(operateType = CREATE)
//    public CommonResult<Integer> createUp(@Valid @RequestBody FollowUpSaveReqVO createReqVO) {
//        return success(upService.createUp(createReqVO));
//    }

//    @PutMapping("/update")
//    @Operation(summary = "更新随访列表")
//    @ApiAccessLog(operateType = UPDATE)
//    @PreAuthorize("@ss.hasPermission('follow:up:update')")
//    public CommonResult<Boolean> updateUp(@Valid @RequestBody FollowUpSaveReqVO updateReqVO) {
//        upService.updateUp(updateReqVO);
//        return success(true);
//    }

//    @DeleteMapping("/delete")
//    @Operation(summary = "删除随访列表")
//    @ApiAccessLog(operateType = DELETE)
//    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('follow:up:delete')")
//    public CommonResult<Boolean> deleteUp(@RequestParam("id") Integer id) {
//        upService.deleteUp(id);
//        return success(true);
//    }

    @GetMapping("/get")
    @Operation(summary = "获得随访列表详情")
    @ApiAccessLog(operateType = GET)
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('follow:up:details')")
    public CommonResult<FollowUpRespVO> getUp(@RequestParam("id") Integer id) {
        FollowUpDO up = upService.getUp(id);
        return success(BeanUtils.toBean(up, FollowUpRespVO.class));
    }

    @PostMapping("/page")
    @Operation(summary = "获得随访列表分页")
    @ApiAccessLog(operateType = GET)
    @PreAuthorize("@ss.hasPermission('follow:up:query')")
    public CommonResult<PageResult<FollowUpRespVO>> getUpPage(@RequestBody @Valid FollowUpPageReqVO pageReqVO) {
        PageResult<FollowUpDO> pageResult = upService.getUpPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FollowUpRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出随访Excel模板")
    @PreAuthorize("@ss.hasPermission('follow:up:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUpExcel(@Valid FollowUpPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<FollowUpDO> list = upService.getUpPage(pageReqVO).getList();
        ArrayList<FollowUpExcelVO> list = new ArrayList<>();
        // 导出 Excel
        ExcelUtils.write(response, "随访列表.xls", "数据", FollowUpExcelVO.class,
                        BeanUtils.toBean(list, FollowUpExcelVO.class));
    }

    @PostMapping("/import-follow")
    @Operation(summary = "导入随访数据")
    @ApiAccessLog(operateType = IMPORT)
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    @PreAuthorize("@ss.hasPermission('follow:up:import')")
    public CommonResult<ImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                  @RequestParam(value = "updateSupport", defaultValue = "false") Boolean updateSupport) throws IOException, InterruptedException {
        TemplateCheckListener<FollowUpExcelVO> listener = new TemplateCheckListener<>(Config.PATIENT_FOLLOW_UP);
        List<FollowUpExcelVO> list = ExcelUtils.read(file, FollowUpExcelVO.class,listener);
        return success(upService.importExcel(list));
    }

}